/**
 * 运营日志表格
 */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Table } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import { defaultData } from '../../services/device';
import getPagination from '../../components/pagination';


const styles = {
	icon: {
		fontSize: '16px',
		marginRight: '1em'
	}
};

const columns = [
	{
		title: '设备状态',
		dataIndex: 'workStatusDesc',
		key: 'workStatusDesc',
	},
	{
		title: '发生时间',
		render: (text, record) => {
			return moment(record.ctime).format('YYYY-MM-DD HH:mm:ss');
		},
		key: 'ctime'
	},
	{
		title: '报文',
		dataIndex: 'content',
		key: 'content'
	}
];

class RunningRecordTable extends Component {
	constructor(props, context) {
		super(props, context);
		this.state = {
			queryData: {
				query: { deviceSno: props.device.detailData.sno },
				...defaultData.query,
				orderByField: 'ctime',
				asc: false
			},
			pagination: {
				current: 0,
				total: 0
			}
		};
	}

	componentWillMount() {
		this.query();
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.device.runningRecordList) {
			this.setState({
				pagination: this.props.device.runningRecordPagination
			});
		}
	}

	query() {
		// 请求数据
		const { dispatch } = this.props;
		dispatch({
			type: 'device/queryRunningRecord',
			payload: { ...this.state.queryData }
		});
	}

	pageChange = page => {
		const { queryData } = this.state;
		this.setState({
			queryData: { ...queryData, current: page }
		}, () => this.query());
	}

	pagesizgeChange = (current, size) => {
		const { queryData } = this.state;
		this.setState({
			queryData: { ...queryData, current: 1, size }
		}, () => this.query());
	}

	render() {
		const { edit, total, page, loading, selectKeys, device, ...props } = this.props;
		const { pagination } = this.state;
		const rowSelection = edit ? {
			onChange: this.onChange,
			selectedRowKeys: selectKeys
		} : null;

		return (
			<div>
				<Table
					dataSource={device.runningRecordList}
					columns={columns}
					pagination={getPagination({
						current: pagination.current,
						onChange: this.pageChange,
						total: pagination.total,
						onShowSizeChange: this.pagesizgeChange
					})}
					rowSelection={rowSelection}
					{...props}
					rowKey="id"
				/>
			</div>
		);
	}
}

RunningRecordTable.propTypes = {

};

export default connect(
	({ device, loading }) => ({ device, loading })
)(RunningRecordTable);

